package com.dtsz.m2.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.model.Users;
import com.dtsz.m2.ui.view.MyDialog;
import com.dtsz.m2.utils.RegluarUtils;
import com.dtsz.m2.utils.StringUtils;
import com.dtsz.m2.volley.state.HUCState;

import java.util.HashMap;
import java.util.Map;
public class RegisterActivity extends BaseActivity {
    private EditText etMail, etPwd, etPwdEnd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {
        super.initView();
        etMail = findViewById(R.id.et_mail);
        etPwd = findViewById(R.id.et_pwd);
        etPwdEnd = findViewById(R.id.et_pwd_end);
    }

    @Override
    public void initClick() {
        super.initClick();
        findViewById(R.id.bt_pwd).setOnClickListener(this);
        findViewById(R.id.tv_break).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_pwd:
                String mail = etMail.getText().toString();
                String pwd = etPwd.getText().toString();
                String pwdEnd = etPwdEnd.getText().toString();
                if (StringUtils.isEmpty(mail)) {
                    showToast("账号不能为空！");
                    return;
                }
                if (mail.length() <= 3) {
                    showToast("账号最少4位");
                    return;
                }
                if (!RegluarUtils.isLetterDigit(pwd)) {
                    showToast("密码长度必须8~16位，并且包含字母和数字");
                    return;
                }
                if (!pwd.equals(pwdEnd)) {
                    showToast("两次输入密码必须一致");
                    return;
                }
                setSharedPreferences("etMail", mail);
                setSharedPreferences("pwdEnd", pwdEnd);
                pageVo.setEmail(mail);

//                View view = getLayoutInflater().inflate(R.layout.item_verification, null);
//                builder = new MyDialog(this, view, R.style.DialogTheme);
//                WebView webView = view.findViewById(R.id.bw_verification);
//                webView.clearCache(true);
//                webView.getSettings().setJavaScriptEnabled(true);
//                webView.setWebViewClient(new WebViewClient());
//                webView.addJavascriptInterface(new vaptchaInterface(), "vaptchaInterface");
//                webView.loadUrl(UrlConfig.URL_M2_DEV + "index.html");
//                builder.show();

                hucRequestInterfase.executeStr(this, Users.class,
                        HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.ISACTIVATIONUSER_URL,
                        hucParameInterfase.getRequestParameter(pageVo), true);
                break;
            case R.id.tv_break:
                finish();
                break;
        }
    }


    private void getClick() {
        loading();
        hucRequestInterfase.executeStr(this, Users.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.ISACTIVATIONUSER_URL,
                hucParameInterfase.getRequestParameter(pageVo), true);
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                String msg = (String) response;
                if ("通过".equals(msg)) {
                    finish();
                    sendIntent(this, RegisterTowActivity.class);
                } else {
                    showToast(msg);
                }
                break;
            case HUCState.HUC_REQEUST_CODE_3:
                String msg1 = response.toString();
                if ("成功".equals(msg1)) {
                    getClick();
                } else {
                    showToast(msg1);
                }
                break;
        }
        loadDismiss();
    }
//    public class vaptchaInterface {
//        @JavascriptInterface
//        public void signal(String json, String token, String sign) {
//            try {
//                runOnUiThread(() -> {
//                    System.out.println(json);
//                    System.out.println(token);
//                    System.out.println(sign);
//                    Map<String, String> mapJson = new HashMap<>();
//                    builder.dismiss();
//                    mapJson.put("nc_token", token);
//                    mapJson.put("csessionid", json);
//                    mapJson.put("sig", sign);
//                    mapJson.put("phone", etMail.getText().toString());
//                    hucRequestInterfase.executeStr(RegisterActivity.this, String.class, HUCState.HUC_REQEUST_CODE_3, Request.Method.POST, UrlConfig.FIT_AUTH_URL, mapJson, true);
////                    builder.dismiss();
//                });
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }
}